package com.issac.vhr.controller.system.emp;

import com.issac.vhr.entity.*;
import com.issac.vhr.service.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import utils.POIUtils;

import java.io.IOException;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/employee/basic")
public class EmployeeBasicController {

    @Autowired
    EmployeeService employeeService;
    @Autowired
    NationService nationService;
    @Autowired
    PoliticsstatusService politicsstatusService;
    @Autowired
    PositionService positionService;
    @Autowired
    JobLevelService jobLevelService;
    @Autowired
    DepartmentService departmentService;
    @GetMapping("/")
        public RespPageBean getAllEmployee(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size, Employee employee, Date[] beginDate){
        return employeeService.getEmployeeByPage(page,size,employee,beginDate);
    }
    @PostMapping("/")
    public RespBean addEmp(@RequestBody Employee employee) {
        if (employeeService.addEmp(employee)==1){
            return RespBean.ok("添加成功");
        }
        return RespBean.error("添加失败");
    }
    @GetMapping("/nations")
    public List<Nation> getAllNation(){
        return nationService.getAllNations();
    }
    @GetMapping("/politicsstatus")
    public List<Politicsstatus> getAllPoliticsStatus(){
        return politicsstatusService.getAllPoliticsStatus();
    }
    @GetMapping("/positions")
    public List<Position> getAllPosition(){
        return positionService.getAllPosition();
    }
    @GetMapping("/jobLevels")
    public List<JobLevel> getAllJobLevel(){
        return jobLevelService.getAllJobLevel();
    }

    @GetMapping("/MaxWorkID")
    public String getMaxWorkID(){
        return employeeService.getMaxWorkID();
    }
    @GetMapping("/deps")
    public List<Department> getDeps(){
        //从根目录-1开始查
        return departmentService.getAllDepartmentByParentId(-1);
    }
    @PutMapping("/")
    public RespBean updateEmp(@RequestBody Employee employee){
        if (employeeService.updateEmp(employee)==1){
            return RespBean.ok("更新成功");
        }
        return RespBean.error("更新失败");
    }
    @DeleteMapping("/{id}")
    public RespBean deleteEmpById(@PathVariable Integer id){
        if (employeeService.deleteEmpById(id)==1){
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }

    //Excel导出

    /*
    *   根据搜索记录导出还没做
    *
    * */
    @GetMapping("/export")
    public ResponseEntity<byte []> exportData(@Param("name") String name){
        //查询所有员工数据
        List<Employee> employee = (List<Employee>) getAllEmployee(null,null,null,null).getData();

        return POIUtils.employee2Excel(employee);
    }


    @PostMapping("/import")
    public RespBean importData(MultipartFile file) throws IOException {
        List<Employee> employeesList = POIUtils.excel2Employee(file, nationService.getAllNations(),politicsstatusService.getAllPoliticsStatus(),departmentService.getAllDepartment(),positionService.getAllPosition(),jobLevelService.getAllJobLevel());
        if (employeeService.addEmps(employeesList)==employeesList.size()){
            return RespBean.ok("上传成功");
        }
        return RespBean.error("上传失败");

    }

}
